package jp.co.itfrontier.mashup.command {
	import jp.co.itfrontier.mashup.event.IEvent;
	import jp.co.itfrontier.mashup.core.ModelLocater;
	import jp.co.itfrontier.mashup.vo.BookVO;
	import mx.logging.Log;
	import flash.net.getClassByAlias;
	import jp.co.itfrontier.mashup.core.Logger;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.Event;
	import jp.co.itfrontier.mashup.utils.CrossDomainUtil;
	import flash.net.sendToURL;
	import jp.co.itfrontier.mashup.event.ChangeModelEvent;
	import jp.co.itfrontier.mashup.core.EventController;

	public class YahooAPI {
		private const Yahoo_url:String = "http://api.jlp.yahoo.co.jp/MAService/V1/parse";
		private const Yahoo_appid:String = "wildgeese";
		private const Yahoo_results:String = "ma";
		private const Yahoo_filter:String = "9";
		private const Yahoo_response:String = "surface,pos";
		

		public function MASearch(sentense:String):void{
			//探してみます						
			var urlVar:URLVariables = new URLVariables();

			urlVar.sentence = sentense;
			urlVar.results = Yahoo_results;
			urlVar.ma_filter = Yahoo_filter;
			urlVar.response = Yahoo_response;
			urlVar.appid = Yahoo_appid;
			
			// データを取得するURLを保持するクラス
			var urlRequest:URLRequest;

			urlRequest = new URLRequest();
			urlRequest.url = Yahoo_url;
			urlRequest.data = urlVar;

			var proxy:CrossDomainUtil = new CrossDomainUtil();
			proxy.requestProxy(urlRequest, responseCompleteHandler);

			Logger.getInstance().info("yahoo::execute");
			
		}
		
		private function responseCompleteHandler(event:Event):void {

			var urlLoader:URLLoader = event.target as URLLoader;
			
			var resultXML:XML = new XML(urlLoader.data);

			var yahoo:Namespace = new Namespace("urn:yahoo:jp:jlp");

			var yahooArray:Array = new Array();

			for each (var x:XML in resultXML..yahoo::surface) {
				yahooArray.push(x.toString());
			}

			ModelLocater.getInstance().yahooWords = yahooArray;

			Logger.getInstance().info("yahoo:" + yahooArray.join(" | "));

			EventController.getInstance().dispatchEvent(new ChangeModelEvent(ChangeModelEvent.YAHOO));

		}

	}
}